<?php
require_once '_inc.php';

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	$title = $_POST['title'];
	$content = $_POST['content'];
	$keywords = $_POST['keywords'];
	$description = $_POST['description'];
	$category = $_POST['category'];
	$copyfrom = $_POST['copyfrom'];
	$author   = $_POST['author'];
	$id       = intval($_POST['id']);
	$seokey   = $_POST['seokeywords'];
	$seodes   = $_POST['seodescription'];
	if( $id ) {
		$sql = "update ".$gDb['prefix']."news set title='$title', 
												  content='$content', 
												  keywords='$keywords',
												  description='$description',
												  catid='$category',
												  seokeywords='$seokey',
												  seodescription='$seodes',
												  copyfrom='$copyfrom', author='$author' where id='$id'";
	} else {
		$now = time();
		$sql = "insert into ".$gDb['prefix']."news (`catid`,`title`,`description`,`seokeywords`,`seodescription`,`content`,`copyfrom`,`author`,`keywords`,`click`,`created`) values ('$category','$title','$description','$seokey','$seodes','$content','$copyfrom','$author','$keywords',0,'$now')";
	}
	if( !$db->query($sql) ) {
		echo mysql_errno().mysql_error();
		exit();
	}
	if($_POST['backUrl'])
	{
		header("location: ".base64_decode($_POST['backUrl']));
		exit();
	}
	header("location: news_list.php");
	exit();
}

$cats = $db->fetchrows("select * from ".$gDb['prefix']."categories");
$tpl->assign('cats', $cats);

$op = $_GET['op'];
switch($op)
{
	case 'add':
		$tpl->display('news_edit.tpl');
		break;
	
	case 'edit':
		$id = intval($_GET['id']);
		if( $id )
		{
			$news = $db->fetchrow("select * from ".$gDb['prefix']."news where id=$id");
			$tpl->assign('news',$news);
			$tpl->display('news_edit.tpl');
		}
		break;
	
	case 'del':
		{
			$id = intval($_GET['id']);
			if( $id )
			{
				if($db->query("delete * from ".$gDb['prefix']."news where id=$id"))
				{
					if($_GET['backUrl'])
					{
						header("location: ".base64_decode($_POST['backUrl']));
						exit();
					}
					header("location: news_list.php");
					exit();
				} else {
					echo mysql_errno(). mysql_error();
					exit( );
				}
			}
		}
		break;
}